package com.ybkj.o2o.mc.manager.impl;

import com.ybkj.o2o.mc.common.DataGrid;
import com.ybkj.o2o.mc.common.transport.PassengerAddressResult;
import com.ybkj.o2o.mc.entity.PassengerAddress;
import com.ybkj.o2o.mc.manager.PassengerAddressManager;
import com.ybkj.o2o.mc.service.PassengerAddressService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by shine on 15/8/19.
 */
@Service
public class DefaultPassengerAddressManager implements PassengerAddressManager {


  @Autowired
  private PassengerAddressService passengerAddressService;

  @Override
  public boolean exists(String address, Integer type, Long passengerId) {

    return passengerAddressService.exists(address, type, passengerId);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public PassengerAddress insert(PassengerAddress passengerAddress) {

    passengerAddressService.insert(passengerAddress);

    return passengerAddress;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int delete(Long addressId, Long passengerId) {

    return passengerAddressService.delete(addressId, passengerId);
  }

  @Override
  public PassengerAddress find(Long addressId, Long passengerId) {

    return passengerAddressService.find(addressId, passengerId);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(PassengerAddress address) {

    return passengerAddressService.update(address);
  }

  @Override
  public PassengerAddress findByPassengerAndType(Long passengerId, Integer type) {

    return passengerAddressService.findByPassengerAndType(passengerId, type);
  }

  @Override
  public List<PassengerAddress> findByPassenger(Long passengerId) {

    return passengerAddressService.findByPassenger(passengerId);
  }


  @Override
  public DataGrid<PassengerAddressResult> findByPassengerForPage(Long passengerId,
                                                                 int page,
                                                                 int rows) {

    int offset = (page - 1) * rows;

    List<PassengerAddress> list = passengerAddressService.findByPassenger(passengerId,
            offset, rows);
    long total = passengerAddressService.countByPassenger(passengerId);

    DataGrid<PassengerAddressResult> dataGrid = new DataGrid<>();
    dataGrid.setRows(PassengerAddressResult.convertFrom(list));
    dataGrid.setTotal(total);
    return dataGrid;
  }

}
